package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.admin.service.IIndexService;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.entity.LegalAreas;
import com.mdd.common.entity.LegalCase;
import com.mdd.common.entity.LegalMoneyRecord;
import com.mdd.common.entity.MediationRecord;
import com.mdd.common.entity.system.SystemAuthAdmin;
import com.mdd.common.entity.user.User;
import com.mdd.common.mapper.LegalAreasMapper;
import com.mdd.common.mapper.LegalCaseMapper;
import com.mdd.common.mapper.LegalMoneyRecordMapper;
import com.mdd.common.mapper.MediationRecordMapper;
import com.mdd.common.mapper.system.SystemAuthAdminMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.util.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 主页服务实现类
 */
@Service
public class IndexServiceImpl implements IIndexService {

    @Resource
    LegalAreasMapper legalAreasMapper;
    @Resource
    LegalMoneyRecordMapper legalMoneyRecordMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    LegalCaseMapper legalCaseMapper;
    @Resource
    MediationRecordMapper mediationRecordMapper;
    @Resource
    SystemAuthAdminMapper systemAuthAdminMapper;


    /**
     * 控制台数据
     *
     * @author fzr
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> console() {
        Map<String, Object> console = new LinkedHashMap<>();

        // 版本信息
        Map<String, Object> version = new LinkedHashMap<>();
        version.put("name", ConfigUtils.get("website", "name", "LikeAdmin-Java"));
        version.put("version", GlobalConfig.version);
        version.put("website", "www.likeadmin.cn");
        version.put("based", "Vue3.x、ElementUI、MySQL");
        Map<String, String> channel = new LinkedHashMap<>();
        channel.put("gitee", "https://gitee.com/likeadmin/likeadmin_java");
        channel.put("website", "https://www.likeadmin.cn");
        version.put("channel", channel);
        console.put("version", version);

        // 今日数据
        Map<String, Object> today = new LinkedHashMap<>();
        today.put("time", "2022-08-11 15:08:29");
        today.put("todayVisits", 10);   // 访问量(人)
        today.put("totalVisits", 100);  // 总访问量
        today.put("todaySales", 30);    // 销售额(元)
        today.put("totalSales", 65);    // 总销售额
        today.put("todayOrder", 12);    // 订单量(笔)
        today.put("totalOrder", 255);   // 总订单量
        today.put("todayUsers", 120);   // 新增用户
        today.put("totalUsers", 360);   // 总访用户
        console.put("today", today);

        // 访客图表
        Map<String, Object> visitor = new LinkedHashMap<>();
        visitor.put("date", TimeUtils.daysAgoDate(15));
        visitor.put("list", Arrays.asList(12,13,11,5,8,22,14,9,456,62,78,12,18,22,46));
        console.put("visitor", visitor);

        return console;
    }

    @Override
    public Map<String, Object> console2() throws ParseException {
        // 版本信息
        Map<String, Object> version = new LinkedHashMap<>();
        // 平台总收益
        LegalAreas legalAreas = legalAreasMapper.selectOne(new QueryWrapper<LegalAreas>()
                .eq("type", 1)
                .last("limit 1"));
        BigDecimal pt_total = legalAreas.getAmount();
        pt_total = pt_total.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP);

        // 平台今日收益
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Date todayStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format + " 00:00:00");
        Date todayEnd   = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(format + " 23:59:59");
        List<LegalMoneyRecord> legalMoneyRecords_list = legalMoneyRecordMapper.selectList(new QueryWrapper<LegalMoneyRecord>()
                .eq("a_id", 0)
                .eq("user_id", 0)
                .eq("type", 1)
                .between("create_time", todayStart.getTime() / 1000, todayEnd.getTime() / 1000)
        );
        BigDecimal pt_today = new BigDecimal("0.0");
        for (LegalMoneyRecord item : legalMoneyRecords_list) {
            if (item.getAmount() != null) {
                pt_today = pt_today.add(new BigDecimal(item.getAmount()));
            }
        }
        pt_today = pt_today.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP);

        //管理公司总收益
        List<LegalAreas> legalAreas_list = legalAreasMapper.selectList(new QueryWrapper<LegalAreas>()
                .eq("type", 2));
        BigDecimal areas_total = new BigDecimal("0.0");
        for (LegalAreas item : legalAreas_list) {
            if (item.getAmount() != null) {
                areas_total = areas_total.add(item.getAmount());
            }
        }
        areas_total = areas_total.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP);

        //管理公司今日收益
        List<LegalMoneyRecord> legalMoneyRecords_list2 = legalMoneyRecordMapper.selectList(new QueryWrapper<LegalMoneyRecord>()
                .gt("a_id", 0)
                .eq("user_id", 0)
                .eq("type", 1)
                .between("create_time", todayStart.getTime() / 1000, todayEnd.getTime() / 1000)
        );
        BigDecimal areas_today = new BigDecimal("0.0");
        for (LegalMoneyRecord item : legalMoneyRecords_list2) {
            if (item.getAmount() != null) {
                areas_today = areas_today.add(new BigDecimal(item.getAmount()));
            }
        }
        areas_today = areas_today.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP);

        //管理公司
        Integer areas_num = legalAreas_list.size();
        //执行公司
        Integer zx_areas_num = 0;
        //代理公司
        Integer dl_areas_num = 0;
        //注册用户
        List<User> user_list = userMapper.selectList(new QueryWrapper<User>()
                .eq("is_delete", 0));
        Integer user_num = user_list.size();
        //律师
        List<User> user_list2 = userMapper.selectList(new QueryWrapper<User>()
                .eq("is_delete", 0)
                .eq("is_lawyer", 1)
        );
        Integer lawyer_num = user_list2.size();
        //人民调解员
        Integer rm_tjy_num = 0;

        // 案件总金额
        List<LegalCase> legalCases_list = legalCaseMapper.selectList(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .in("status", 3, 4, 5, 6, 7)
        );
        BigDecimal entrust_case_total_amount = new BigDecimal("0.0");
        for (LegalCase item : legalCases_list) {
            if (item.getPrice() != null) {
                entrust_case_total_amount = entrust_case_total_amount.add(new BigDecimal(item.getPrice()));
            }
        }
        entrust_case_total_amount = entrust_case_total_amount.divide(BigDecimal.valueOf(10000), 2, BigDecimal.ROUND_HALF_UP);
        // 案件总数量
        Integer entrust_case_total = legalCases_list.size();

        //委托总数
        Integer total = legalCases_list.size();
        //今日案件
        Long today = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .between("create_time", todayStart.getTime() / 1000, todayEnd.getTime() / 1000)
        );
        // 待发布案件
        Long dfb = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .in("status", 0, 1, 2)
        );
        //待接单(待接案件)
        Long djd = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .eq("status", 3)
        );
        //已接单(已接案件)
        Long yjd = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .in("status", 4, 5, 6, 7)
        );
        //发布案件
        Integer yfb = legalCases_list.size();
        //待付款(待付案件)
        Long dfk = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .eq("status", 5)
        );
        //已完结(完结案件)
        Long ywc = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .eq("status", 7)
        );
        //委托临期
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        Date endDate = cal.getTime();
        Long wtlq = legalCaseMapper.selectCount(new QueryWrapper<LegalCase>()
                .eq("delete_time", 0)
                .eq("status", 7)
                .le("end_time", endDate.getTime() / 1000)
        );
        // 饼图
        List<Object> pie = new LinkedList<>();
        Map<String, Object> map1 = new LinkedHashMap<>();
        map1.put("name", "待发布");
        map1.put("value", dfb);
        pie.add(map1);
        Map<String, Object> map2 = new LinkedHashMap<>();
        map2.put("name", "待接单");
        map2.put("value", djd);
        pie.add(map2);
        Map<String, Object> map3 = new LinkedHashMap<>();
        map3.put("name", "待付款");
        map3.put("value", dfk);
        pie.add(map3);
        Map<String, Object> map4 = new LinkedHashMap<>();
        map4.put("name", "已完结");
        map4.put("value", ywc);
        pie.add(map4);


        // 案件数量(调解总数)
        Long case_num = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
        );
        // 今日案件数量(今日调解总数)
        Long case_today_num = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
                .ge("creation_time", todayStart)
        );
        // 书记员数量
        Long system_auth_num = systemAuthAdminMapper.selectCount(new QueryWrapper<SystemAuthAdmin>()
                .eq("is_disable", 0)
                .eq("is_delete", 0)
        );
        // 待调解
        Long dtj = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
                .eq("mediation_result", 0)
        );
        // 调解成功
        Long tjcg = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
                .eq("mediation_result", 1)
        );
        // 调解失败
        Long tjsb = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
                .in("mediation_result", 2, 5)
        );
        // 二次调解
        Long ectj = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
                .eq("is_first_mediation", 0)
        );
        // 司法处理
        Long sfcl = mediationRecordMapper.selectCount(new QueryWrapper<MediationRecord>()
                .eq("status", 1)
                .eq("is_judicial_treatment", 1)
        );

        version.put("pt_total", pt_total);
        version.put("pt_today", pt_today);
        version.put("areas_total", areas_total);
        version.put("areas_today", areas_today);
        version.put("areas_num", areas_num);
        version.put("zx_areas_num", zx_areas_num);
        version.put("dl_areas_num", dl_areas_num);
        version.put("user_num", user_num);
        version.put("lawyer_num", lawyer_num);
        version.put("rm_tjy_num", rm_tjy_num);
        version.put("entrust_case_total_amount", entrust_case_total_amount);
        version.put("entrust_case_total", entrust_case_total);
        version.put("total", total);
        version.put("today", today);
        version.put("yfb", yfb);
        version.put("djd", djd);
        version.put("yjd", yjd);
        version.put("dfk", dfk);
        version.put("ywc", ywc);
        version.put("wtlq", wtlq);
        version.put("case_num", case_num);
        version.put("case_today_num", case_today_num);
        version.put("system_auth_num", system_auth_num);
        version.put("dtj", dtj);
        version.put("tjcg", tjcg);
        version.put("tjsb", tjsb);
        version.put("ectj", ectj);
        version.put("sfcl", sfcl);
        version.put("pie", pie);


        return version;
    }

    /**
     * 公共配置
     *
     * @author fzr
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> config() {
        Map<String, String> website   = ConfigUtils.get("website");
        String copyright = ConfigUtils.get("website", "copyright", "");

        String captchaStatus = YmlUtils.get("like.captcha.status");

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("loginCaptcha", StringUtils.isNotNull(captchaStatus) && captchaStatus.equals("true"));
        map.put("webName", website.getOrDefault("name", ""));
        map.put("webLogo", UrlUtils.toAbsoluteUrl(website.getOrDefault("logo", "")));
        map.put("webFavicon", UrlUtils.toAbsoluteUrl(website.getOrDefault("favicon", "")));
        map.put("webBackdrop", UrlUtils.toAbsoluteUrl(website.getOrDefault("backdrop", "")));
        map.put("ossDomain", UrlUtils.domain());
        map.put("copyright", ListUtils.stringToListAsMapStr(copyright));

        return map;
    }

}
